<template>
  <div class="event-detail-box">
    <!-- 指派详情---未指派--去指派。指派后--回显指派信息 -->
    <div class="step-top-info">
      <div class="step-box">
        <div class="header-step">
          <img class="step-img" src="../../assets/images/finally.png" />
          <span class="step-title">研判信息</span>
          <span class="line"></span>
        </div>
        <div class="step-content">
          <p class="info-text">
            <span>研判人员：{{ eventInfo.judgementMemberName || "-" }}</span>
          </p>
          <p class="info-text">
            <span>研判时间：{{ eventInfo.judgementDatetime || "-" }}</span>
          </p>
          <p class="info-text">
            <span>研判结果：{{ eventInfo.judgementResult || "-" }}</span>
          </p>
          <p class="info-text">
            <span
              >责任科室：{{
                eventInfo.judgementResponsibleOrgName || "-"
              }}</span
            >
          </p>
        </div>
      </div>
      <div class="step-box" v-if="eventInfo.eventType == '10'">
        <div class="header-step">
          <span class="step-num" v-if="eventInfo.eventStatus == '0'">2</span>
          <img class="step-img" v-else src="../../assets/images/finally.png" />
          <span class="step-title">指派信息</span>
          <span class="line"></span>
        </div>
        <div class="step-content">
          <p class="info-text">
            <span>指派人：{{ eventInfo.assignMemberName || "-" }}</span>
          </p>
          <p class="info-text">
            <span>指派时间：{{ eventInfo.assignTime || "-" }}</span>
          </p>
          <p class="info-text">
            <span>责任人：{{ eventInfo.assignedMemberName || "-" }}</span>
          </p>
        </div>
      </div>
      <div class="step-box">
        <div class="header-step">
          <span class="step-num" v-if="eventInfo.eventStatus == '0'">3</span>
          <img class="step-img" v-else src="../../assets/images/finally.png" />
          <span class="step-title">处置信息</span>
        </div>
        <div class="step-content">
          <p class="info-text">
            <span>处置状态：{{ eventInfo.eventStatusValue || "-" }}</span>
          </p>
          <p class="info-text">
            <span>处置人：{{ eventInfo.handleUser || "-" }}</span>
          </p>
          <p class="info-text">
            <span>处置时间：{{ eventInfo.handleTime || "-" }}</span>
          </p>
          <p class="info-text">
            <span
              >处置详情：<el-button
                link
                style="color: #2263bf"
                @click="showDetail"
                >查看详情</el-button
              ></span
            >
          </p>
        </div>
      </div>
    </div>
    <div class="bottom-info">
      <div class="left-info">
        <div class="video-box">
          <div style="height: 350px">
            <videoRtsp
              v-if="
                !showVideo &&
                currentInfo.deviceOriginCode &&
                !showpreve &&
                !showDetailVisable &&
                !showDigVisable
              "
              @showql="showql"
              :Times="currentInfo.reportTime"
              :vid="currentInfo.deviceOriginCode"
            />
            <div class="border-top" :style="borderStop"></div>
          </div>
          <div class="right-event-btn">
            <div
              class="eIndex"
              :class="{ active: currentInfo.id == item.id }"
              v-for="(item, index) in eventlist"
              :key="item.id"
              @click="selectEvent(item)"
            >
              {{ index + 1 }}
            </div>
          </div>
        </div>
        <!-- 详细信息 -->
        <div class="baseinfo">
          <el-tabs v-model="activeName" class="demo-tabs">
            <el-tab-pane label="事件信息" name="base">
              <p class="textinfo">
                事件类型：{{ currentInfo.eventCodeDisplay }}
              </p>
              <p class="textinfo">上报时间：{{ currentInfo.reportTime }}</p>
              <p class="textinfo">
                事件等级：{{ currentInfo.eventClassDisplay }}
              </p>
              <p class="textinfo">所属社区：{{ currentInfo.orgName }}</p>
              <p class="textinfo">报警点位：{{ currentInfo.deviceName }}</p>
              <p class="textinfo">点位编号：{{ currentInfo.deviceId }}</p>
              <p class="textinfo">事件地址：{{ currentInfo.address }}</p>
               <template v-if="currentInfo.sensorInfo">
                <p class="textinfo">
                  当前值：{{ currentInfo.sensorInfo.curValue }}
                </p>
                <p class="textinfo">
                  报警阀值：{{ currentInfo.sensorInfo.thresholdValue }}
                </p>
                <p class="textinfo">
                  传感器编号：{{ currentInfo.sensorInfo.sensorId }}
                </p>
                <p class="textinfo">
                  传感器类型：{{ currentInfo.sensorInfo.sensorName }}
                </p>
              </template>
              <p class="textinfo">
                抓拍照片：<img
                  :src="currentInfo.pic"
                  @click="showImg(currentInfo.pic)"
                />
              </p>
              <div
                class="textinfo"
                v-if="
                  eventInfo.eventCode &&
                  (eventInfo.eventCode.indexOf('1001') > -1 ||
                    eventInfo.eventCode.indexOf('1002') > -1)
                "
              >
                录像回放：
                <div @click="showbk">
                  <videoplayBk :url="videoUrl"></videoplayBk>
                </div>
              </div>
              <!-- <p class="playbackbtn" @click="showbk">录像回放</p> -->
            </el-tab-pane>
            <!-- <el-tab-pane label="规则信息" name="gz"></el-tab-pane> -->
          </el-tabs>
        </div>
        <div
          class="zj-info"
          v-if="
            eventInfo.eventCode &&
            (eventInfo.eventCode.indexOf('1001') > -1 ||
              eventInfo.eventCode.indexOf('1002') > -1)
          "
        >
          <div
            class="confirm-btn"
            @click="save"
            v-if="UserId == '1903999283766628353'"
          >
            一键取证
          </div>
          <div class="img-list">
            <div v-if="menuId !== '1904018483650367488'" style="display: flex">
              <div
                class="box"
                v-for="(item, index) in downlist.slice(0, 2)"
                :key="index"
              >
                <img class="img" :src="item.url" @click="showImg(item.url)" />
                <p class="name">{{ item.name }}</p>
              </div>
            </div>
            <div
              class="box"
              style="cursor: pointer"
              v-show="downlist.length > 2"
              @click="getVideo(downlist[2].url)"
            >
              <videoplayBk
                :capUrl="downlist.length > 1 && downlist[1].url ? false : true"
                @capImg="capImgData"
                :url="downlist.length > 2 ? downlist[2].url : ''"
              ></videoplayBk>
            </div>
          </div>
        </div>
      </div>
      <!-- 指挥中心不显示指派，选项框 -->
      <div
        class="right-info"
        v-if="eventInfo.eventStatus == '0' && UserId !== '1903999283766628357'"
      >
        <zp :eventInfo="eventInfo"></zp>
      </div>
    </div>
    <!-- 处置详情 -->
    <SelfDialog
      :dialogVisable="showDetailVisable"
      title="处置详情"
      @close="showDetailVisable = false"
      :showFooter="false"
      :isright="false"
    >
      <template #default>
        <div class="detail-content">
          <p class="detail-title">远程驱离</p>
          <p class="text">驱离记录：{{ eventInfo.handlerTime || "-" }}</p>
          <p class="text">驱离次数：{{ eventInfo.handlerTimes || "-" }}</p>
          <p class="text">驱离人员:{{ eventInfo.person || "-" }}</p>
          <p class="text">处置人员：{{ eventInfo.handleUser || "-" }}</p>
          <p class="text">处置时间：{{ eventInfo.handlerTime || "-" }}</p>
          <p class="detail-title mt10">处置内容</p>
          <p class="text">{{ eventInfo.handlerRemark || "-" }}</p>
          <p class="detail-title mt10">处置照片</p>
          <div class="img-list">
            <img
              :key="index"
              v-for="(item, index) in eventInfo.handlerPic"
              :src="item"
            />
          </div>
        </div>
      </template>
    </SelfDialog>

    <!-- 视频播放 -->
    <SelfDialog
      style="width: 600px"
      :dialogVisable="showDigVisable"
      title="视频播放"
      @close="showDigVisable = false"
      :showFooter="false"
      :isright="false"
    >
      <template #default>
        <div style="width: 400px; height: 400px; margin: 20px auto">
          <videoplayBk class="bigbk" :url="videoUrl"></videoplayBk>
        </div>
      </template>
    </SelfDialog>

    <vue-easy-lightbox
      teleport-to="body"
      :visible="showpreve"
      :index="0"
      :imgs="urlCatch"
      class="lightbox-override"
      @hide="showpreve = false"
    />
  </div>
</template>

<script setup>
import zp from "./zp.vue";
import videoRtsp from "@/components/video/videoRtsp.vue";
import videoplayBk from "@/components/video/videobk.vue";
import videoplay from "@/components/video/video.vue";
import SelfDialog from "@/components/dialog/dialog.vue";
import {
  getEventDetail,
  getOneKeyCheck,
  getplaybackcameraurl60s,
} from "@/api/event.js";
import { nextTick, onMounted, ref } from "vue";
import { useRoute } from "vue-router";
import { Minxfun } from "@/utils/minxin.js";
import { useUserStore } from "@/pinia/user.js";
import { getmenuListFun } from "@/api/index.js";
import VueEasyLightbox from "vue-easy-lightbox";
import axios from "axios";
const route = useRoute();
const { showImg, urlCatch, showpreve } = Minxfun();

const piniaUser = useUserStore();
const menuId = ref(null);
const UserId = localStorage.getItem("User");
// 获取菜单列表
const fetchMenuList = async () => {
  try {
    const menuData = await getmenuListFun();
    if (menuData.data.menuList && menuData.data.menuList.length > 0) {
      menuId.value = menuData.data.menuList[0].menuCode;
    }
  } catch (error) {}
};

onMounted(() => {
  fetchMenuList();
  getDetailData();
});

// 事件包列表
let eventlist = ref([]);
let currentInfo = ref({});
let eventInfo = ref({});
let showDetailVisable = ref(false);

// 用于 一键取证下的video放大回显
let showDigVisable = ref(false);
let videoUrl = ref("");

let showVideo = ref(false);
let snapList = ref([
  {
    x: "0.1",
    y: "0.2",
    w: "0.2",
    h: "0.3",
  },
]);
let currentBorder = ref(-1);
let borderStop = ref({
  width: 0,
  height: 0,
  opacity: 0,
});
let downlist = ref([]);

const activeName = ref("base");

// 获取案件基本信息
const getDetailData = async () => {
  try {
    const { data } = await getEventDetail({
      busizEventId: route.query.busizEventId,
    });
    eventlist.value = data.events;
    currentInfo.value = data.events[0] || {};
    eventInfo.value = {
      ...data,
      handlerPic: data.handlerPic ? data.handlerPic.split(",") : [],
    };
    console.log("eventInfo", eventInfo.value);
    if (
      data.eventCode &&
      (data.eventCode.indexOf("1001") > -1 ||
        data.eventCode.indexOf("1002") > -1)
    ) {
      getshowbk(currentInfo.value);
    }
  } catch (error) {
    console.error(error);
  }
};

// 选择展示的案件
const selectEvent = (item) => {
  currentInfo.value = item;
  getshowbk(currentInfo.value);
};

// 获取回放url
const getshowbk = async (item) => {
  try {
    const { data } = await getplaybackcameraurl60s({
      eventAllInfoId: item.id || "",
      // role: localStorage.getItem("User"),
      cameraId: item.deviceId || "",
      uuid: "",
      previewTimeDurning: 60000,
      postTimeDurning: 60000,
    });
    if (!data.playBackVideoUrl) return;
    nextTick(() => {
      videoUrl.value = data.playBackVideoUrl;
    });
  } catch (e) {
    console.error(e);
  }
};

// 展示回放
const showbk = async () => {
  try {
    const { data } = await getOneKeyCheck({
      busizEventId: route.query.busizEventId,
      role: localStorage.getItem("User"),
      deviceId: currentInfo.value.deviceId,
      uuid: "",
      previewTimeDurning: 60000,
      postTimeDurning: 60000,
    });
    if (!data.playBackVideoUrl) return;

    showDigVisable.value = true;

    nextTick(() => {
      videoUrl.value = data.playBackVideoUrl;
    });
  } catch (e) {
    console.error(e);
  }
};

// 展示video
const getVideo = async (item) => {
  const { data } = await getOneKeyCheck({
    busizEventId: route.query.busizEventId,
    role: localStorage.getItem("User"),
    deviceId: currentInfo.value.deviceId,
    uuid: "",
  });
  if (!data.playBackVideoUrl) return;
  // console.log('getVideo called with item:', item);

  showDigVisable.value = true;

  nextTick(() => {
    videoUrl.value = data.playBackVideoUrl;
  });

  // console.log('videoUrl set to:', videoUrl.value);
  // console.log('showDigVisable set to:', showDigVisable.value);
};

// 选择border
const selectBorder = (index, item) => {
  if (currentBorder.value == index) {
    currentBorder.value = -1;
    borderStop.value = {
      width: 0,
      height: 0,
      opacity: 0,
    };
  } else {
    currentBorder.value = index;
    borderStop.value = {
      width: 640 * item.w + "px",
      height: 360 * item.h + "px",
      left: 640 * item.x + "px",
      top: 360 * item.y + "px",
    };
  }
};

// 远程驱离
const showql = () => {
  showVideo.value = true;
};

const confirm = () => {
  showVideo.value = false;
};

const close = () => {
  showVideo.value = false;
};

const startTalk = () => {
  playvideoDom.value.startTalk();
};

// 一键取证
const save = async () => {
  try {
    const { data } = await getOneKeyCheck({
      busizEventId: route.query.busizEventId,
      role: localStorage.getItem("User"),
      deviceId: currentInfo.value.deviceId,
      uuid: "",
    });
    if (!data.playBackVideoUrl) return;
    downlist.value = [
      {
        name: "远景照片",
        url: data.distantViewPicUrl,
      },
      {
        name: "近景照片",
        url: data.closeShotUrl,
      },
      {
        name: "录像",
        url: data.playBackVideoUrl,
      },
    ];
  } catch (e) {
    console.error(e);
  }
};

// 处置详情弹框
const showDetail = () => {
  showDetailVisable.value = true;
};

// 截图
const capImgData = async (base) => {
  try {
    if (!base) return;
    let file = await base64ToFile(base, "小图.png", "PNG");
    const formData = new FormData();
    formData.append("file", file);
    formData.append("busizEventId", route.query.busizEventId);
    const { data } = await axios.post(
      "/integrated-workbench/v1/enforcement/picuploadwithbusizeventid",
      formData,
      {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      }
    );
    console.log("ssssssssssss", data.data.picPath);
    downlist.value[1] = {
      name: "近景图片",
      url: data.data.picPath,
    };
  } catch (e) {
    console.error(e);
  }
};

// 将Base64字符串转换为File对象
function base64ToFile(base64, filename, mimeType) {
  const byteCharacters = atob(base64.split(",")[1]);
  const byteNumbers = new Array(byteCharacters.length);
  for (let i = 0; i < byteCharacters.length; i++) {
    byteNumbers[i] = byteCharacters.charCodeAt(i);
  }
  const byteArray = new Uint8Array(byteNumbers);
  const blob = new Blob([byteArray], { type: mimeType });
  return new File([blob], filename, { type: mimeType });
}

let playvideoDom = ref();
</script>

<style lang="scss">
.lightbox-override {
  z-index: 9999 !important;
}
.event-detail-box {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  .step-top-info {
    width: 100%;
    height: 170px;
    background: #ffffff;
    box-shadow: 0px 0px 29px 0px rgba(70, 102, 149, 0.06);
    border-radius: 12px;
    display: flex;
    align-items: top;
    padding: 20px 50px;
    .step-box {
      flex: 1;
      height: 100%;
      margin-right: 30px;
      &:nth-child(4) {
        margin-right: 0;
      }
      .header-step {
        display: flex;
        align-items: center;
        .step-num {
          display: inline-block;
          width: 32px;
          height: 32px;
          background: #2263bf;
          text-align: center;
          line-height: 32px;
          color: #ffffff;
          margin-right: 17px;
          border-radius: 50%;
          &.normal {
            background: rgba(142, 191, 255, 0.72);
            border: 1px solid #2c84ff;
            color: #2263bf;
          }
        }
        .step-img {
          display: inline-block;
          width: 32px;
          height: 32px;
          margin-right: 17px;
        }
        .step-title {
          display: inline-block;
          width: auto;
          font-family: PingFangSC, PingFang SC;
          font-weight: 500;
          font-size: 16px;
          color: #333333;
          line-height: 22px;
          margin-right: 40px;
        }
        .line {
          flex: 1;
          height: 1px;
          background: rgba(34, 99, 191, 0.2);
        }
      }
      .step-content {
        padding-left: 50px;
        .info-text {
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 14px;
          color: #999999;
          line-height: 25px;
        }
      }
    }
  }
  .bottom-info {
    width: 100%;
    height: calc(100% - 198px);
    margin-top: 20px;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    .left-info {
      flex: 1;
      height: 100%;
      background: #ffffff;
      box-shadow: 0px 0px 29px 0px rgba(70, 102, 149, 0.06);
      border-radius: 6px;
      padding: 36px 25px 0 35px;
      .video-box {
        display: inline-block;
        vertical-align: top;
        width: 640px;
        height: 350px;
        position: relative;
        .right-event-btn {
          position: absolute;
          width: 40px;
          right: -40px;
          top: 0;
          height: 100%;
          overflow-y: auto;
          overflow-x: hidden;
          &::-webkit-scrollbar {
            width: 5px;
          }
          &::-webkit-scrollbar-thumb {
            background-color: #2263bf;
            border-radius: 10px;
          }
          .eIndex {
            width: 30px;
            height: 36px;
            background: rgba(142, 191, 255, 0.22);
            border-radius: 0px 3px 3px 0px;
            border: 1px solid #2c84ff;
            font-size: 14px;
            color: #2263bf;
            line-height: 36px;
            text-align: center;
            margin-bottom: 5px;
            cursor: pointer;
            &.active {
              background: #2263bf;
              color: #fff;
            }
          }
        }
        .border-top {
          position: absolute;
          border: 1px solid red;
        }
      }
      .baseinfo {
        display: inline-block;
        vertical-align: top;
        width: calc(100% - 700px);
        margin-left: 57px;
        .textinfo {
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 14px;
          color: #666666;
          line-height: 29px;
          text-align: left;
          margin-bottom: 10px;
          display: flex;
          align-items: top;
          img {
            width: 146px;
            height: 82px;
          }
        }
        .labeltitle {
          font-family: PingFangSC, PingFang SC;
          font-weight: 500;
          font-size: 16px;
          margin-top: 18px;
          margin-bottom: 18px;
        }
        .eyebtn {
          width: 128px;
          height: 40px;
          border: 1px solid #eeeeee;
          margin-right: 10px;
          line-height: 40px;
          display: flex;
          align-items: center;
          justify-content: space-around;
          cursor: pointer;
          padding: 0 12px;
        }
      }
      .video-cover {
        width: 100%;
        height: 40px;
        background: rgba(142, 191, 255, 0.22);
        display: flex;
        align-items: center;
        justify-content: space-around;
        line-height: 40px;
        .btnv {
          display: flex;
          align-items: center;
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 14px;
          color: #666666;
          cursor: pointer;
          img {
            margin-right: 11px;
          }
        }
      }
    }
    .right-info {
      width: 520px;
      height: 100%;
      background: #ffffff;
      box-shadow: 0px 0px 29px 0px rgba(70, 102, 149, 0.06);
      border-radius: 6px;
      margin-left: 20px;
    }
  }
  .zj-info {
    width: 640px;
    height: 200px;
    margin-top: -150px;
    overflow: auto;
    position: relative;
    .confirm-btn {
      width: 143px;
      height: 36px;
      background: #2263bf;
      border-radius: 4px;
      font-size: 12px;
      color: #ffffff;
      line-height: 36px;
      cursor: pointer;
      text-align: center;
      position: relative;
      &::after {
        width: 300px;
        content: "默认下载内容:事发前后10s视频，事件截图";
        position: absolute;
        right: -280px;
        top: 0px;
        font-family: PingFangSC, PingFang SC;
        font-weight: 400;
        font-size: 12px;
        color: #999999;
      }
    }
    .img-list {
      margin-top: 15px;
      display: flex;
      align-items: center;
      .box {
        width: 200px;
        height: 117px;
        margin-right: 8px;
        .img {
          width: 200px;
          height: 100px;
        }
        .name {
          text-align: center;
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 14px;
          color: #666666;
        }
      }
    }
  }
}
.el-tabs__nav-wrap:after {
  height: 1px !important;
  background: rgba(34, 99, 191, 0.2) !important;
}
.el-tabs__active-bar {
  background: #2263bf !important;
}
.el-tabs__item {
  font-family: PingFangSC, PingFang SC;
  font-weight: 500;
  font-size: 16px;
  color: #333333;
}
.el-tabs__item.is-active,
.el-tabs__item:hover {
  color: #2263bf !important;
}
.el-tab-pane {
  padding-left: 10px;
}

.video-dialog {
  padding-left: 20px;
  height: 100%;
  display: inline-block;
  vertical-align: top;
  position: absolute;
  left: -420px;
  top: 0px;
}
.right-yzinfo {
  display: inline-block;
  vertical-align: top;
  width: 330px;
  height: 100%;
  padding-top: 40px;
  margin-left: 40px;
  .yzimg {
    margin-left: 100px;
    margin-bottom: 18px;
    width: 50px;
    height: 50px;
  }
  .text {
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 14px;
    color: #666666;
    line-height: 24px;
  }
  .panel-btn {
    display: flex;
    align-items: center;
    justify-content: space-around;
    cursor: pointer;
    .btn-l {
      width: 45%;
      display: flex;
      align-items: center;
      color: #2263bf;
      img {
        margin-right: 10px;
      }
    }
    .btn-r {
      width: 40%;
      height: 36px;
      background: #2263bf;
      border-radius: 4px;
      font-size: 12px;
      color: #ffffff;
      line-height: 36px;
      text-align: center;
      cursor: pointer;
    }
  }
}
</style>

<style lang="scss" scoped>
.detail-content {
  padding-top: 0 !important;
  .detail-title {
    font-family: PingFangSC, PingFang SC;
    font-weight: 500;
    font-size: 14px;
    color: #333333;
    line-height: 20px;
    margin-bottom: 10px;
  }
  .text {
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 14px;
    color: #666666;
    line-height: 24px;
  }
  .img-list {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    img {
      margin-right: 10px;
      width: 31%;
      height: 100px;
    }
  }
}
</style>
